package entity

import "cmp"

type BinaryTree[T cmp.Ordered] struct {
	*PNode[T]
}

func (t *BinaryTree[T]) CountNodes() int {
	if t == nil || t.PNode == nil {
		return 0
	}
	if t.IsLeaf() {
		return 1
	}
	return 1 + t.Left.ChildBinaryTree().CountNodes() + t.Right.ChildBinaryTree().CountNodes()
}
func (t *BinaryTree[T]) CountLeafs() int {
	if t == nil || t.PNode == nil {
		return 0
	}
	if t.IsLeaf() {
		return 1
	}
	return t.Left.ChildBinaryTree().CountLeafs() + t.Right.ChildBinaryTree().CountLeafs()
}
func (t *BinaryTree[T]) CountNonLeafNodes() int {
	if t == nil {
		return 0
	}
	if t.IsLeaf() {
		return 0
	}
	return 1 + t.Left.ChildBinaryTree().CountNonLeafNodes() + t.Right.ChildBinaryTree().CountNonLeafNodes()
}
func (t *BinaryTree[T]) RightSubTree(data T) *BinaryTree[T] {
	if t == nil {
		return nil
	}
	if t.Data == data {
		return t.Right.ChildBinaryTree()
	} else if t.Data < data {
		return t.Right.ChildBinaryTree().RightSubTree(data)
	} else {
		return t.Left.ChildBinaryTree().RightSubTree(data)
	}
}

//=======
